package com.neusoft.neuiotms.dcom.controller;

import java.util.Optional;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.constants.DCOMConstants;
import com.neusoft.neuiotms.dcom.dto.QuestionnaireDTO;
import com.neusoft.neuiotms.dcom.dto.QuestionnaireFeedbackDTO;
import com.neusoft.neuiotms.dcom.service.QuestionnaireService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "任务（调查问卷）管理")
@RestController
@RequestMapping(value = "/api/w/questionnires")
public class QuestionnaireController {

    @Autowired
    private QuestionnaireService questionnaireService;

    @ApiOperation("查询所有任务")
    @GetMapping
    public ResultListDTO<QuestionnaireDTO> index(final Searchable searchable, final UserBean userBean) {
        return ResultListDTO.success(this.questionnaireService.index(searchable, userBean));
    }

    @ApiOperation("分页查询所有任务")
    @GetMapping("/p")
    public PageResultDTO<QuestionnaireDTO> search(final Pageable pageable, final Searchable searchable,
            final UserBean userBean) {
        return PageResultDTO.success(this.questionnaireService.search(pageable, searchable, userBean));
    }

    @ApiOperation("创建任务")
    @PostMapping
    public ResultDTO<QuestionnaireDTO> create(@RequestBody final QuestionnaireDTO dto) {
        return ResultDTO.success(this.questionnaireService.create(dto));
    }

    @ApiOperation("发布任务")
    @PutMapping("/{id}/publish")
    public ResultDTO<?> publish(@PathVariable final Long id) {
        return ResultDTO.success(this.questionnaireService.publish(id));
    }

    @ApiOperation(value = "修改任务")
    @PutMapping("/{id}")
    public ResultDTO<QuestionnaireDTO> update(@PathVariable final Long id,
            @RequestBody final QuestionnaireDTO dto) {
        return ResultDTO.success(this.questionnaireService.update(id, dto));
    }

    @ApiOperation("任务详情")
    @GetMapping("/{id}")
    public ResultDTO<QuestionnaireDTO> details(@PathVariable final Long id, final UserBean userBean) {
        final QuestionnaireDTO questionnaireDTO = this.questionnaireService.details(id);
        // 一线工程师只能看到自己的总结
        if (userBean.getRoles().stream().filter(it -> it.getRole().equals(DCOMConstants.ROLE_WORKER)).findAny()
                .isPresent()) {
            final Optional<QuestionnaireFeedbackDTO> opt = questionnaireDTO.getFeedbacks().stream()
                    .filter(f -> f.getUsername().equals(userBean.getUsername())).findAny();
            questionnaireDTO.getFeedbacks().clear();
            if (opt.isPresent()) {
                questionnaireDTO.getFeedbacks().add(opt.get());
            }
        } else {
            questionnaireDTO.setFeedbacks(questionnaireDTO.getFeedbacks().stream().filter(it -> it.isSubmitted())
                    .collect(Collectors.toList()));
        }
        return ResultDTO.success(questionnaireDTO);
    }

    @ApiOperation("删除任务")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.questionnaireService.delete(id);
        return ResultDTO.success();
    }
}
